package com.gsn.chooseRoom;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.TableModel;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.GsnException;
import com.gsn.engine.template.GsnLabel;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ButtonRoom {
	GsnImageButton chooseRoom;
	int index;
	long gold;
	Label labelName;
	Label labelBet;
	Label labelNum;
	Label labelIndex;
	public float width;
	public float height;

	public ButtonRoom(int index, float y) {
		this.index = index;
		PortalTexture portalTexture = PortalTexture.inst();
		// TODO Auto-generated constructor stub
		chooseRoom = new GsnImageButton(portalTexture.chooseRoomButton, portalTexture.chooseRoomButtonPress);
		portalTexture.scaleActor(chooseRoom);
		chooseRoom.y = y;

		TableModel table = ConfigModel.tableModelList.get(index);
		gold = table.bet;
		// labelIndex = new Label(ActorUtility.formatNumber(table.index), new
		// Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0,
		// 1)));
		labelIndex = new Label(ActorUtility.formatNumber(table.index), new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0, 1)));
		// labelGold = new Label("1000", new
		// Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1,
		// 1)));

		labelIndex.width = chooseRoom.getRealWidth() * 0.15f;
		labelIndex.setAlignment(Align.CENTER);
		labelIndex.x = chooseRoom.getRealWidth() * 0.00f;
		labelIndex.y = (chooseRoom.getRealHeight() - labelIndex.height) * 0.5f + y;

		// labelName = new Label(table.tableName, new
		// Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0,
		// 1)));
		labelName = new Label(table.tableName, new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0, 1)));

		labelName.setAlignment(Align.CENTER);
		labelName.width = chooseRoom.getRealWidth() * 0.34f;
		labelName.x = chooseRoom.getRealWidth() * 0.2f;
		labelName.y = labelIndex.y;

		labelBet = new Label(ActorUtility.formatNumber(table.bet), new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0, 1)));

		labelBet.setAlignment(Align.CENTER);
		labelBet.width = chooseRoom.getRealWidth() * 0.24f;
		labelBet.x = chooseRoom.getRealWidth() * 0.54f;
		labelBet.y = labelIndex.y;

		if (table.type == 0) {
			labelNum = new Label(String.valueOf(table.personCount) + "/5(tập sự)", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0, 1)));
		} else {
			labelNum = new Label(String.valueOf(table.personCount) + "/5(nâng cao)", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(0, 0, 0, 1)));
		}

		labelNum.setAlignment(Align.CENTER);
		labelNum.width = chooseRoom.getRealWidth() * 0.21f;
		labelNum.x = chooseRoom.getRealWidth() * 0.78f;
		labelNum.y = labelIndex.y;

		chooseRoom.setClickListener(new ClickListener() {

			@Override
			public void click(Actor actor, float x, float y) {
				// TODO Auto-generated method stub
				PokerSound.playClick();
				if (!MyPoker.game.statePortal.equals(StatePortal.CHOOSE_ROOM))
					return;
				MyPoker.game.statePortal = StatePortal.CHOOSING_ROOM;
				try {
					MyPoker.game.playScreen.finishInit();
				} catch (GsnException ex) {
					ex.printStackTrace();
				}
				MyPoker.game.chooseRoomScreen.setDefaultInputListener();
				UtilityGettor.inst().showProgressDialog("Vào bàn", "Đang vào bàn");
				MyPoker.client.send(PacketFactory.chooseRoom(ButtonRoom.this.index));

			}
		});

		width = chooseRoom.getRealWidth();
		height = chooseRoom.getRealHeight();

	}

	public void setInfo(long gold, int personCount, String name, int type, int index) {
		labelBet.setText(ActorUtility.formatNumber(gold));
		if (type == 0) {
			labelNum.setText(String.valueOf(personCount) + "/5 (tập sự)");
		} else {
			labelNum.setText(String.valueOf(personCount) + "/5 (nâng cao)");
		}
		labelName.setText(name);
		labelIndex.setText(String.valueOf(index));
	}

	public void addActor(Group group) {
		group.addActor(chooseRoom);
		group.addActor(labelBet);
		group.addActor(labelIndex);
		group.addActor(labelName);
		group.addActor(labelNum);

	}
}
